<template>
    <div class="components_edit_modal">
        <el-dialog width="500px" :title="title" :visible.sync="state" :show-close="true" :close-on-click-modal="false">
            <el-form ref="messagesr" :model="messagesr" :rules="rules" label-width="95px" size="mini">

                <el-form-item label="标题：" prop="title">
                    <el-input v-model.trim="messagesr.title"></el-input>
                </el-form-item>

                <el-form-item label="消息内容：" prop="alert">
                    <el-input v-model.trim="messagesr.alert"></el-input>
                </el-form-item>

                <el-form-item label="额外参数：" prop="extras">
                    <el-input v-model.trim="messagesr.extras"></el-input>
                    <p style="font-size: 14px; color: red;">请注意填写格式！</p>
                </el-form-item>

                <el-form-item label="是否推送：" prop="isPush">
                    <el-switch
                        v-model="messagesr.isPush"
                        active-text="是"
                        inactive-text="否">
                    </el-switch>
                </el-form-item>

                <el-form-item label="消息平台：" prop="platform">
                    <el-radio v-model="messagesr.platform" label="android">android</el-radio>
                    <el-radio v-model="messagesr.platform" label="iOS">iOS</el-radio>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" size="small" @click="addmessagesr">确 定</el-button>
                <el-button @click="state = false"  size="small">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {enums} from '@/constant'
    export default {
        data() {

            return {
                state: false,
                title: '',
                type:'',
                messagesr:{
                    alert: null,
                    extras: null,
                    isPush: true,
                    title: null,
                    platform:null
                },
                rules: {
                    alert: [
                        { required: true, message: '请输入内容', trigger: 'blur'}
                    ],
                    // extras: [
                    //     { required: true, message: '请输入额外参数', trigger: 'blur'}
                    // ],
                    title: [
                        { required: true, message: '请输入标题', trigger: 'blur'}
                    ],
                    platform: [
                        { required: true, message: '请选择平台', trigger: 'blur'}
                    ]
                },
            }
        },
        created() {

        },
        mounted() {},
        methods: {
            showDialog(val){

                if(this.$refs['messagesr']!==undefined){
                    this.$refs['messagesr'].resetFields();
                }

                if(val){
                    for(var key in val){
                       this.messagesr[key] = val[key];
                    }
                    this.title = "编辑系统消息";
                    this.type = "edit";
                }else{
                    this.title = "添加系统消息";
                    this.type = "add";
                }

                this.state = true;
            },
            addmessagesr(){
                let vm = this;
                vm.$refs.messagesr.validate((valid) => {
                    if (valid) {

                        let submission =  ''; //提交方式

                        //选择提交方式
                        if(vm.type == 'edit'){
                            submission = 'put';
                        }else{
                            submission = 'post';
                        }
                        // 提交
                        this.axios({
                            method: submission,
                            url: api.messagesrManagement.messagesrList,
                            data: vm.messagesr
                        }).then((response) => {
                            if (response.data.code == 200) {
                                vm.state = false;
                                vm.$parent.$refs.messagesrTable.$emit('vuetable:refresh');
                                vm.$message.success(response.data.message);
                            } else {
                                vm.$message.error(response.data.message);
                            }
                        })
                    }else{
                        return false;
                    }
                });
            }
        },


    };
</script>
<style scoped>
.dialog-footer{ text-align:center; }
.dialog-footer button{ margin: 0 10%; }
</style>